import { setToken, getToken } from '@/utils/auth'
import { sysLogin, sysProfile, sysUser } from '@/api/user.js'

const state = {
  token: getToken() || '',
  userInfo: ''
}
const mutations = {
  setToken (state, value) {
    state.token = value
    setToken(value)
  },
  setUserInfo (state, value) {
    state.userInfo = value
  },
  // 清除用户信息与token
  logout (state) {
    state.userInfo = ''
    state.token = ''
    setToken('')
  }
}
const actions = {
  async toLogin (store, form) {
    const res = await sysLogin(form)
    store.commit('setToken', res.data)
  },
  // 获取用户信息
  async getUserInfo ({ commit }) {
    const res = await sysProfile()
    const res2 = await sysUser(res.data.userId)
    // 合并对象 {...a,...b}
    // 返回值（合并后的目标对象）= Object.assign(目标对象，需要合并的对象，...)
    const res3 = Object.assign({}, res.data, res2.data)
    console.log(res, res2, res3)
    commit('setUserInfo', res3)
  }
}
const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
